package com.ruoyi.ichen.rabbitmq.day3;

import com.rabbitmq.client.*;
import com.ruoyi.ichen.rabbitmq.common.RabbitMQUtils;

import java.io.IOException;

/**
 * @author ichen
 * @version V1.0
 * @ClassName: Customer1
 * @Description: TODO(用一句话描述该类)
 * @Date 2020/8/10 23:09
 */
public class Customer2 {

    public static void main(String[] args) throws IOException {
        //获取连接对象
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();

        //通道绑定交换机
        channel.exchangeDeclare("ichen","fanout");

        //临时队列
        String queue = channel.queueDeclare().getQueue();

        //绑定交换机和队列
        channel.queueBind(queue,"ichen","");

        //消费消息
        channel.basicConsume(queue,true,new DefaultConsumer(channel){
            @Override // 最后一个参数 消息队列中取出的消息
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
                System.out.println("消费者2:" + new String(body));
            }
        });


    }
}
